package airthmetic.exercise.string;

import java.util.*;

public class _451_根据字符出现频率排序 {

    public String frequencySort(String s) {
        PriorityQueue<String> queue = new PriorityQueue<String>((s1, s2)->{
            return Integer.valueOf(s2.substring(2)).intValue() - Integer.valueOf(s1.substring(2)).intValue();
        });

        HashMap<Character, Integer> map = new HashMap();
        char[] cs = s.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            if (map.containsKey(cs[i])) {
                map.put(cs[i], map.get(cs[i]) + 1);
            } else {
                map.put(cs[i], 1);
            }
        }

        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Character, Integer>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<Character, Integer> next = iterator1.next();
            queue.offer(next.getKey() + "," + next.getValue());
        }

        StringBuilder sb = new StringBuilder();
        while(!queue.isEmpty()){
            String keyWord = queue.poll();
            String count = keyWord.substring(2);
            int i = new Integer(count).intValue();
            for(int j=0; j<i; j++){
                sb.append(keyWord.substring(0,1));
            }
        }

        return sb.toString();


    }


    public static void main(String[] args) {

        _451_根据字符出现频率排序 v = new _451_根据字符出现频率排序();

        System.out.println(v.frequencySort("efefefff"));

     /*   PriorityQueue<String> queue = new PriorityQueue<String>((s1, s2)->{
            return new Integer(s2.substring(2)).intValue() - new Integer(s1.substring(2)).intValue();
        });

        String a = "e,3";
        String b = "f,5";


        queue.offer(a);
        queue.offer(b);
        StringBuilder sb = new StringBuilder();
        while(!queue.isEmpty()){
            String keyWord = queue.poll();
            String count = keyWord.substring(2);
            int i = new Integer(count).intValue();
            for(int j=0; j<i; j++){
                sb.append(keyWord.substring(0,1));
            }
        }
        System.out.println(sb.toString());*/

    }
}
